package com.biz.primus.management.coupon.type.controller;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.coupon.type.feign.CouponTypeFeignClient;
import com.biz.primus.model.coupon.vo.couponType.CouponTypeDetailVo;
import com.biz.primus.model.coupon.vo.couponType.CouponTypeVo;
import com.biz.primus.model.coupon.vo.couponType.request.QueryCouponTypeProductsVo;
import com.biz.primus.model.coupon.vo.couponType.request.QueryCouponTypeReqVo;
import com.biz.primus.model.coupon.vo.couponType.response.CouponTypeProductVo;
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import static com.biz.primus.model.coupon.exceptions.CouponTypeExceptionType.CLOUD_SERVICE_UNAVAILABLE;

/**
 * 优惠券类型Controller
 *
 * @author david-liu
 */
@Controller
@RequestMapping("/management/couponType")
public class CouponTypeController extends AbstractBaseController {

    @Autowired
    private Optional<CouponTypeFeignClient> couponTypeFeignClientOptional;

    @GetMapping
    public ModelAndView couponTypeList(QueryCouponTypeReqVo reqVo) {
        ModelAndView view = new ModelAndView("coupon_type/list");
        CouponTypeFeignClient couponTypeFeignClient = this.getCouponTypeFeignClient();
        SpringPageVO<CouponTypeDetailVo> page = couponTypeFeignClient.listCouponTypes(Optional.ofNullable(reqVo.getCouponTypeName()).orElse(""), reqVo.getPage() - 1, reqVo.getSize());
        view.addObject("page", page);
        return view;
    }

    @GetMapping(value = "new")
    public ModelAndView couponTypeNew() {
        return new ModelAndView("coupon_type/add");
    }

    @PostMapping(value = "save")
    public ModelAndView couponTypeSave(CouponTypeVo couponTypeVo) {
        CouponTypeFeignClient couponTypeFeignClient = this.getCouponTypeFeignClient();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        couponTypeVo.setLastUpdateUser(authentication.getName());
        couponTypeVo.setCreator(authentication.getName());
        Boolean result = couponTypeFeignClient.createCouponType(couponTypeVo);
        ModelAndView view;
        if (result) {
            view = new ModelAndView("redirect:/management/couponType.do");
        } else {
            view = new ModelAndView("redirect:/management/couponType/new.do").addObject("errMsg", "创建优惠券类型出错");
        }
        return view;
    }

    @ResponseBody
    @GetMapping(value = "/products")
    public String couponProducts(QueryCouponTypeProductsVo vo) {
        List<CouponTypeProductVo> voList = Lists.newArrayList();
        IntStream.rangeClosed(1, 10000).forEach(index -> {
            CouponTypeProductVo couponTypeProductVo = new CouponTypeProductVo();
            couponTypeProductVo.setId((long) index);
            couponTypeProductVo.setName("name" + index);
            couponTypeProductVo.setCategory("category" + index);
            couponTypeProductVo.setBrand("brand" + index);
            voList.add(couponTypeProductVo);
        });
        Map<String, Object> result = new HashMap<String, Object>() {
            private static final long serialVersionUID = 248507072493224852L;

            {
                put("total", voList.size());
                put("rows", voList.subList((vo.getPageNumber() - 1) * vo.getPageSize(), vo.getPageNumber() * vo.getPageSize() - 1));
            }
        };
        return JsonUtils.obj2Json(result);
    }

    private CouponTypeFeignClient getCouponTypeFeignClient() {
        CouponTypeFeignClient couponTypeFeignClient = couponTypeFeignClientOptional.orElse(null);
        AssertUtils.notNull(couponTypeFeignClient, CLOUD_SERVICE_UNAVAILABLE);
        return couponTypeFeignClient;
    }
}
